package com.psbc.redis_study.controller;

import com.google.code.kaptcha.Producer;
import com.psbc.redis_study.utils.CommonUtil;
import com.psbc.redis_study.utils.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.util.concurrent.TimeUnit;

/**
 * 获取图形验证码
 * 首先要有生成验证码的工具类，还有就是我们在向redis存储验证码的时候要有一个key，但是这个
 * key一定要唯一，我们通过用户ip和请求头信息一起组成来生成key
 * 然后生成验证码的信息，生成key，存到redis中，并且设置过期时间
 * 最后要生成图片，将图片信息返回给用户
 */
@RestController
@RequestMapping("/api/v1/captcha")
public class CaptchaController {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private Producer captchaProducer;

    @GetMapping("/get_captcha")
    public void getCaptcha(HttpServletRequest request, HttpServletResponse response){
        String text = captchaProducer.createText();
        String key = getCaptchaKey(request);
        //10分钟过期
        redisTemplate.opsForValue().set(key,text,10, TimeUnit.MINUTES);
        BufferedImage bufferedImage = captchaProducer.createImage(text);
        ServletOutputStream outputStream = null;
        try{
            outputStream = response.getOutputStream();
            ImageIO.write(bufferedImage,"jpg",outputStream);
            outputStream.flush();
            outputStream.close();
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    /**
     *
     * 发送验证码的逻辑，首先就是要获得redis中的图形验证码，然后与我们输入的进行比较
     * 如果这两个一致，首先应该删除redis中的，不删也可以，有过期时间
     * 然后就应该向用户发送手机验证码了
     * @param to
     * @param captcha
     * @param request
     * @return
     */
    @GetMapping("/send_code")
    public JsonData sendCode(@RequestParam(value = "to",required = true)String to,
                             @RequestParam(value = "captcha",required = true)String captcha,
                             HttpServletRequest request){
        String key = getCaptchaKey(request);
        String cacheCaptcha = redisTemplate.opsForValue().get(key);
        if (captcha!=null && cacheCaptcha != null && cacheCaptcha.equalsIgnoreCase(captcha)){
            redisTemplate.delete(key);
            //发送验证码的逻辑
            //TODO
            return JsonData.buildSuccess();
        }else {
            return JsonData.buildError("验证码不正确，请重新输入");
        }
    }
    public String getCaptchaKey(HttpServletRequest request){
        String ip = CommonUtil.getIpAddr(request);
        String userAgent = request.getHeader("user_Agent");
        String key = "user-service:captcha"+CommonUtil.MD5(ip+userAgent);
        return key;
    }
}
